<?php

namespace common\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\DhMailbox;

/**
 * SearchDhMailbox represents the model behind the search form about `common\models\DhMailbox`.
 */
class SearchDhMailbox extends DhMailbox
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'static', 'reply_id', 'refer_time' ,'status','n_show'], 'integer'],
            [['name', 'phone', 'email', 'code', 'account', 'theme', 'content', 'annex', 'info_code', 'ip','reply_content','update_time','reply_name'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = DhMailbox::find()->orderBy('refer_time DESC');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'type' => $this->type,
            'static' => $this->static,
            'status'=>$this->status,
            'show'=>$this->n_show,
            'reply_id' => $this->reply_id,
            'refer_time' => $this->refer_time,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'account', $this->account])
            ->andFilterWhere(['like', 'theme', $this->theme])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'annex', $this->annex])
            ->andFilterWhere(['like', 'info_code', $this->info_code])
            ->andFilterWhere(['like', 'ip', $this->ip]);

        return $dataProvider;
    }
}
